package io.renren.modules.avanotes.controller;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.vo.VocabularyShowVo;
import io.renren.modules.avanotes.dao.VocabularyDao;
import io.renren.modules.avanotes.entity.MovieSubtitleEntity;
import io.renren.modules.avanotes.entity.SubtitleDetailsEntity;
import io.renren.modules.avanotes.entity.VocabularyEntity;
import io.renren.modules.avanotes.service.MovieSubtitleService;
import io.renren.modules.avanotes.service.SubtitleDetailsService;
import io.renren.modules.avanotes.service.VocabularyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.avanotes.entity.LocalMarkdownNotesEntity;
import io.renren.modules.avanotes.service.LocalMarkdownNotesService;
import springfox.documentation.annotations.ApiIgnore;

import java.util.*;

/**
 * 英语单词词汇表
 *
 * @author zzr
 * @email 529742454@qq.com
 * @date 2022-01-31 17:11:00
 */
@RestController
@RequestMapping("avanotes/vocabulary")
//@Api("英语单词词汇表接口")
public class VocabularyController {
    @Autowired
    private VocabularyService vocabularyService;

    @Autowired
    private VocabularyDao vocabularyDao;

    @Autowired
    private SubtitleDetailsService subtitleDetailsService;

    /**
     * 信息
     */
    @GetMapping("/getRandomWord")
    //@RequiresPermissions("avanotes:vocabulary:info")
    //@ApiOperation("查询单条")
    public R getRandomWord() {
        //

        //VocabularyEntity vocabulary = vocabularyService.getById(1);
        VocabularyEntity vocabulary = vocabularyDao.getNext((long)new Random().nextInt(2614));
        String word = vocabulary.getWord();
        String first = (word.toCharArray()[0] + "").toUpperCase();
        word = first + word.substring(1);

        Map<String, Object> params = new HashMap<>();
        params.put("page", "1");
        params.put("limit", "10");
        params.put("key", word);
        //获取视频列表
        PageUtils pageUtils = subtitleDetailsService.searchPage(params);

        VocabularyShowVo vo = new VocabularyShowVo();
        vo.setWord(vocabulary.getWord());
        vo.setExplanation(vocabulary.getExplanation());
        vo.setList((List<SubtitleDetailsEntity>) pageUtils.getList());
        return R.ok().setData(vo);
    }

    /**
     * 列表
     */
    @PostMapping("/list")
    //@RequiresPermissions("avanotes:vocabulary:list")
    //@ApiImplicitParams({
    //        @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "String"),
    //        @ApiImplicitParam(name = "limit", value = "每页记录数", paramType = "query", dataType = "String"),
    //        @ApiImplicitParam(name = "key", value = "检索关键字", paramType = "query", dataType = "String")
    //})
    //@ApiOperation("查询列表")
    public R list(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageUtils page = vocabularyService.queryPage(params);

        //return R.ok().put("page", page);
        return R.ok().setData(page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("avanotes:vocabulary:info")
    //@ApiOperation("查询单条")
    public R info(@PathVariable("id") Long id) {
            VocabularyEntity vocabulary = vocabularyService.getById(id);

        //return R.ok().put("vocabulary", vocabulary);
        return R.ok().setData(vocabulary);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("avanotes:vocabulary:save")
    //@ApiOperation("保存")
    public R save(@RequestBody VocabularyEntity vocabulary) {
            vocabularyService.save(vocabulary);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    //@RequiresPermissions("avanotes:vocabulary:update")
    //@ApiOperation("修改")
    public R update(@RequestBody VocabularyEntity vocabulary) {
            vocabularyService.updateById(vocabulary);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    //@RequiresPermissions("avanotes:vocabulary:delete")
    //@ApiOperation("删除")
    public R delete(@RequestBody Long[] ids) {
            vocabularyService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}

